package com.wm.wmim.server.handler;

import com.wm.wmim.common.dto.Result;
import com.wm.wmim.common.enums.CommonBizCodeEnum;
import com.wm.wmim.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author weiming
 * @date 2025/2/13 19:00
 * @description
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    // 处理业务异常
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result handleBusinessException(BusinessException exception) {
        log.info("BusinessException: ", exception);
        return Result.error(exception.getCode(), exception.getMsg(), exception.getHttpStatus());
    }

    // 处理系统异常（隐藏堆栈信息）
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception ex) {
        // 记录日志
        log.error("System error:", ex);
        return Result.error(CommonBizCodeEnum.ERROR_UNKNOWN.getCode(), CommonBizCodeEnum.ERROR_UNKNOWN.getMsg(), CommonBizCodeEnum.ERROR_UNKNOWN.getHttpStatus());

    }
}

